﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using IndieStudio.BugsBang.Utility;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Progress))]
	public class Timer : MonoBehaviour
	{
		/// <summary>
		/// Text Component
		/// </summary>
		public Text uiText;

		/// <summary>
		/// The time increment counter text.
		/// </summary>
		public Text timeIncCountText;

		/// <summary>
		/// The time in seconds.
		/// </summary>
		public int timeInSeconds;

		/// <summary>
		/// Whether the timer is paused or not.
		/// </summary>
		private bool isPaused;

		/// <summary>
		/// The time counter.
		/// </summary>
		private float timeCounter;

		/// <summary>
		/// The total time.
		/// </summary>
		private int totalTime;

		/// <summary>
		/// The sleep time.
		/// </summary>
		private float sleepTime;

		/// <summary>
		/// The initial color of the time.
		/// </summary>
		private Color timeInitialColor;

		/// <summary>
		/// Whether to run on start or not.
		/// </summary>
		public bool runOnStart;

		/// <summary>
		/// A static instance of this class.
		/// </summary>
		public static Timer instance;

		void Start(){
			if (runOnStart) {
				Run ();
			}
		}

		void Awake ()
		{
			if (instance == null) {
				instance = this;
			}

			if (uiText == null) {
				uiText = GetComponent<Text> ();
			}

			timeInitialColor = uiText.color;

			Pause ();
		}

		/// <summary>
		/// Run the timer.
		/// </summary>
		public void Run(){
			if (LevelsManager.instance.GetCurrentLevel () == null) {
				return;
			}

			isPaused = false;
			timeCounter = 0;
			sleepTime = 0.01f;
			totalTime =  LevelsManager.instance.GetCurrentLevel().starsTimePeriod * 4;
			timeInSeconds = totalTime;
			CancelInvoke("Wait");
			InvokeRepeating ("Wait", 0, sleepTime);
		}

		/// <summary>
		/// Stop the Timer.
		/// </summary>
		public void Stop ()
		{
			CancelInvoke ();
		}

		/// <summary>
		/// Reset the timer.
		/// </summary>
		public void Reset ()
		{
			Stop ();
			Run ();
		}

		/// <summary>
		/// Pause the Timer.
		/// </summary>
		public void Pause ()
		{
			isPaused = true;
		}

		/// <summary>
		/// Resume the Timer.
		/// </summary>
		public void Resume ()
		{
			isPaused = false;
		}

		/// <summary>
		/// Wait.
		/// </summary>
		private void Wait ()
		{
			if (!isPaused) {
				timeCounter += sleepTime;
				timeInSeconds = totalTime -  (int)timeCounter;
				ApplyTime ();
				Progress.instance.SetProgress (timeCounter);
			}
		}

		/// <summary>
		/// Applies the time into TextMesh Component.
		/// </summary>
		private void ApplyTime ()
		{
			if (uiText == null) {
				return;
			}

			//calculate the minutes
			int mins = (int)(timeInSeconds / 60);

			//calculat the remaining seconds
			int seconds = (int)(timeInSeconds % 60);

			if (timeInSeconds < 11) {
				uiText.color = Colors.redColor;
			} else {
				uiText.color = timeInitialColor;
			}

			uiText.text = GetNumberWithZeroFormat (mins) + ":" + GetNumberWithZeroFormat (seconds);
		}

		/// <summary>
		/// Get the number with zero format.
		/// </summary>
		/// <returns>The number with zero format.</returns>
		/// <param name="number">Ineger Number.</param>
		public static string GetNumberWithZeroFormat (int number)
		{
			string strNumber = "";
			if (number < 10) {
				strNumber += "0";
			}
			strNumber += number;

			return strNumber;
		}

		/// <summary>
		/// Increases the time.
		/// </summary>
		/// <param name="factor">Increment factor.</param>
		public void IncreaseTime (float factor)
		{
			/*
			if (Application.internetReachability == NetworkReachability.NotReachable) {
				Pause ();
				GameManager.instance.isRunning = false;
				GameManager.instance.noConnectionDialog.Show (false);
				return;//skip if there is no connection
			}

			if (incCount == 0) {
				Pause ();
				GameManager.instance.isRunning = false;
				AudioSources.instance.PlayLockedSFX ();
				//Show Increase Time dialog
				//GameManager.instance.increaseTimeDialog.Show (false);
				//TODO: Show market dialog
				AdsManager.instance.ShowAdvertisment (AdPackage.AdEvent.Event.ON_SHOW_NEED_TIME_DIALOG);
				return;
			}
			*/

			if (timeCounter < 1) {
				return;
			}

			timeCounter -= LevelsManager.instance.GetCurrentLevel().starsTimePeriod * factor;
			timeCounter = Mathf.Clamp (timeCounter,0, Mathf.Infinity);
			AudioSources.instance.PlayIncreaseSFX ();
		}

		/// <summary>
		/// Whether the timer is paused.
		/// </summary>
		/// <returns><c>true</c> if the timer is paused; otherwise, <c>false</c>.</returns>
		public bool IsPaused(){
			return isPaused;
		}

		/// <summary>
		/// On destroy event.
		/// </summary>
		void OnDestroy(){
			CancelInvoke ();
		}
	}
}